﻿using Barcode_Project.ProBarPrint;
using Barcode_Project.InitPrint;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using log4net;
using Newtonsoft.Json;
using Barcode_Project.Entity;
using DevExpress.XtraExport.Helpers;
using Barcode_Project.Network;
using DevExpress.XtraEditors;
using Barcode_Project.PurchasePrint;
using DevExpress.XtraBars.Painters;
using Barcode_Project.CutPrint;
using Barcode_Project.MatAuxRePrint;
using Barcode_Project.RollerPrint;
using Barcode_Project.ReBar;
using System.Diagnostics;
using System.Web.UI.WebControls;
using static DevExpress.Utils.Drawing.Helpers.NativeMethods;

namespace Barcode_Project
{
    public partial class Entrance : DevExpress.XtraEditors.XtraForm
    {
        ILog log;
        public Entrance()
        {
            InitializeComponent();
        }
        void CheckDuplicate()
        {
            string processName = Process.GetCurrentProcess().ProcessName;
            Process[] processes = Process.GetProcessesByName(processName);
            //如果该数组长度大于1，说明多次运行
            if (processes.Length > 1)
            {
                XtraMessageBox.Show("程序已运行，请先管理其它窗体！","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                Environment.Exit(0);
            }
        }
        // 根据网页传来的要求，打开不同窗口
        void SwitchWindow(Dictionary<string, string> dic)
        {
            switch (dic["type"].ToString())
            {
                // 生产计划打码
                case "proBarPrint":

                    if (dic["gxCode"].ToString() == "FQ" || dic["gxCode"].ToString().Contains("FQ"))
                    {
                        var cp_win = new CutRollPrint(dic);
                        cp_win.ShowDialog();
                    }
                    else
                    {
                        var pp_win = new PlanPrint(dic);
                        pp_win.ShowDialog();
                    }

                    Close();
                    break;
                // 期初打码
                case "initPrint":
                    var iwp_win = new InWarehousePrint(dic);
                    iwp_win.ShowDialog();

                    Close();
                    break;
                // 采购打码
                case "purchasePrint":
                    var pur_win = new BuyPrint(dic);
                    pur_win.ShowDialog();

                    Close();
                    break;

                case "MatAuxRePrint":
                    var srp_win = new StuffRePrint(dic);
                    srp_win.ShowDialog();

                    Close();
                    break;

                case "PlateRollerPrint":
                    var prp_win = new PlateRollerPrint(dic);
                    prp_win.ShowDialog();

                    Close();
                    break;
                case "rebar":
                    var reWin = new RePrint(dic);
                    reWin.ShowDialog();

                    Close();
                    break; 
                case "gxPrint":
                    var gxPrint = new gxPrint(dic);
                    gxPrint.ShowDialog();

                    Close();
                    break;
                default:
                    break;
            }
        }

        private void button_planPrint_Click(object sender, EventArgs e)
        {
            //生产
           // var arg1 = " {\"type\":\"proBarPrint\",\"userCode\":\"admin\",\"baseCode\":\"002\",\"plCode\":\"PO2411070002\",\"gxCode\":\"YS\",\"gxName\":\"印刷\",\"jfDate\":\"\",\"planNum\":\"100\",\"number\":\"100\",\"proCode\":\"05040001\",\"proName\":\"复合膜成品1\",\"spec\":\"120cm*3c\",\"gxNumber\":\"0\",\"cu_customer\":\"\",\"unitName\":\"个\",\"cu_bcp\":\"\",\"produce_spec\":\"\",\"produce_proName\":\"\",\"produce_unitName\":\"\"}";
            //分切
            var arg1 = "{ \"type\":\"proBarPrint\",\"userCode\":\"admin\",\"baseCode\":\"002\",\"plCode\":\"SO2409290005\",\"gxCode\":\"FQ\",\"gxName\":\"分切\",\"jfDate\":\"2024-09-29\",\"planNum\":\"100.00\",\"number\":\"100.00\",\"proCode\":\"05040001\",\"proName\":\"复合膜成品1\",\"spec\":\"120cm*3c\",\"gxNumber\":\"0\",\"cu_customer\":\"客户1\",\"unitName\":\"个\",\"cu_bcp\":\"030001\",\"produce_spec\":\"\",\"produce_proName\":\"成品测试1\",\"produce_unitName\":\"千克\"}";
            
                var dicList = JsonConvert.DeserializeObject<Dictionary<string, string>>(arg1);
            WebsiteInfo.baseCode = dicList["baseCode"];
            WebsiteInfo.userCode = dicList["userCode"];
            //var win = new PlanPrint(dicList);
            var win = new CutRollPrint(dicList);
            win.ShowDialog();
        }

        private void button_initPrint_Click(object sender, EventArgs e)
        {
            //var win = new InWarehousePrint();
            //win.ShowDialog();
        }

        private void button_purchasePrint_Click(object sender, EventArgs e)
        {
            //var win = new BuyPrint();
            //win.ShowDialog();
        }

        private void button_MatAuxRePrint_Click(object sender, EventArgs e)
        {
            var arg1 = "{\"type\":\"MatAuxRePrint\",\"serverUrl\":\"http://localhost:16989\",\"logLevel\":\"none\",\"userCode\":\"admin\",\"baseCode\":\"002\"}";
            var dicList = JsonConvert.DeserializeObject<Dictionary<string, string>>(arg1);
            var win = new StuffRePrint(dicList);
            win.ShowDialog();
        }

        private void button_plateRollerPrint_Click(object sender, EventArgs e)
        {
            //var win = new PlateRollerPrint();
            //win.ShowDialog();
        }

        private void button_rebar_Click(object sender, EventArgs e)
        {
            //var win = new RePrint(null);
            //win.ShowDialog();
        }

        private void Entrance_Load(object sender, EventArgs e)
        {
            CheckDuplicate();

            // 初始化日志
            log = LogManager.GetLogger(this.GetType().Name);

            string[] args = Environment.GetCommandLineArgs();

            if (args.Length > 1)
            {
                var arg1 = args[1];
                arg1 = arg1.Substring(16, arg1.Length - 16);
                arg1 = HttpUtility.UrlDecode(arg1);
                arg1 = arg1.TrimEnd('/');
                log.Info(arg1);

                //arg1 = " {\"type\":\"proBarPrint\",\"plCode\":\"PO240506002\",\"gxCode\":\"FQ\",\"gxName\":\"分切\",\"jfDate\":\"2024-06-01\",\"number\":\"221\",\"proCode\":\"0201391\",\"proName\":\"印刷级PA膜（同步）\",\"spec\":\"54cm*1.5c\"}\r\n";
                
                var dicList = JsonConvert.DeserializeObject<Dictionary<string, string>>(arg1);
                WebsiteInfo.baseCode = dicList["baseCode"];
                WebsiteInfo.userCode = dicList["userCode"];
                try
                {
                    SwitchWindow(dicList);
                }
                catch (Exception ex)
                {
                    log.Error(ex);
                }

            }

        }
    }
}
